import { defineStore } from 'pinia'
import { getLocalUserState, setLocalUserState, removeUserState } from './helper'


export const useUserStore = defineStore('user-store', {
    actions: {
        /**
         * 获取指定的用户信息
         * @param userId
         */
        getAppointUserInfo():User.saveUserInfo {
           return getLocalUserState();
        },
        /**
         * 获取指定用户的token 信息
         * @param userId
         */
        getAppointUserToken() {
            const userInfo = this.getAppointUserInfo();
            if (userInfo != null) {
                return userInfo.token;
            }
        },
        /**
         * 设置指定用户的token 信息
         * @param userId
         * @param token
         */
        setAppointUserToken(token: string) {
            const userInfo = this.getAppointUserInfo();
            if (userInfo != null) {
                userInfo.token = token;
                this.saveLoginUserInfo(userInfo);
            }
        },
        /**
         * 保存用户信息
         * @param user
         */
        saveLoginUserInfo(user: User.saveUserInfo) {
            if (user != null) {
                setLocalUserState(user);
            }
        },
        /**
         * 移除用户
         * @param userId
         */
        removeUserInfo() {
            removeUserState();
        }

    }
})
